package jp.gr.java_conf.ryz.summarize.client.view;

import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecoration;
import jp.gr.java_conf.ryz.summarize.client.presenter.PostDecorationUtil;
import jp.gr.java_conf.ryz.summarize.client.presenter.PropertyChangedHandler;
import jp.gr.java_conf.ryz.summarize.shared.model.Post;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;

public class PostPanel extends FocusPanel implements ClickHandler, MouseOverHandler
{
	private static final String PostPanelSelectedStyleName = "postPanelSelected";
	private static final String PostPanelUnSelectedStyleName = "postPanelUnSelected";
	
	private Post _post;
	private PostDecoration _decoration;
	private CheckBox _selectedCheck;
	private Panel _bodyPanel;
	
	
	/**
	 * constructor.
	 * 
	 * @param post {@link Post}
	 */
	public PostPanel(Post post)
	{
		_post = post;
		_decoration = new PostDecoration();
		_decoration.addPropertyChangedHandler(new PropertyChangedHandler()
		{
			@Override
			public void OnPropertyChanged(Object sender, String propertyName)
			{
				if (propertyName.equals(PostDecoration.FontSizePropertyName)
						|| propertyName.equals(PostDecoration.IsBoldPropertyName)
						|| propertyName.equals(PostDecoration.ColorPropertyName))
				{
					refreshBodyPanel();
				}
			}
		});
		
		init();
	}
	
	
	@Override
	public void onMouseOver(MouseOverEvent event)
	{
		addStyleName("postPanelMouseOver");
	}

	@Override
	public void onClick(ClickEvent event)
	{
		changeSelectStateInternal(!isSelected());
	}
	
	/**
	 * get post is selected.
	 * 
	 * @return selected is true
	 */
	public boolean isSelected()
	{
		return _selectedCheck.getValue();
	}
	
	/**
	 * set post is selected.
	 * 
	 * @param isSelect select is true
	 */
	public void setSelected(boolean isSelect)
	{
		_selectedCheck.setValue(isSelect);
	}
	
	/**
	 * get post.
	 * 
	 * @return {@link Post}
	 */
	public Post getPost()
	{
		return _post;
	}
	
	/**
	 * get post decoration.
	 * 
	 * @return {@link PostDecoration}
	 */
	public PostDecoration getPostDecoration()
	{
		return _decoration;
	}
	
	/**
	 * change select state.
	 * 
	 * @param isSelect select is true
	 */
	public void changeSelectState(boolean isSelect)
	{
		changeSelectStateInternal(isSelect);
	}
	
	/**
	 * get bold state.
	 * 
	 * @return bold is true
	 */
	public boolean isBold()
	{
		return _decoration.isBold();
	}
	
	/**
	 * set bold state.
	 * 
	 * @param isBold bold is true
	 */
	public void setIsBold(boolean isBold)
	{
		_decoration.setIsBold(isBold);
	}
	
	
	/**
	 * change select state.
	 * 
	 * @param newState new select state (select is true)
	 */
	private void changeSelectStateInternal(boolean newState)
	{
		boolean oldState = !newState;
		if (oldState)
		{
			removeStyleName(PostPanelSelectedStyleName);
			addStyleName(PostPanelUnSelectedStyleName);
		}
		else
		{
			removeStyleName(PostPanelUnSelectedStyleName);
			addStyleName(PostPanelSelectedStyleName);
		}
		
		setSelected(newState);
	}

	/**
	 * initialize.
	 */
	private void init()
	{
		_selectedCheck = new CheckBox();
		_bodyPanel = new SimplePanel();
		
		Panel panel = new FlowPanel();
		
		panel.add(getTitlePanel());
		panel.add(getToolPanel());
		_bodyPanel.add(getBodyPanelContent());
		panel.add(_bodyPanel);
		
		add(panel);
		
		addStyleName("postPanelBase");
		
		addClickHandler(this);
		addMouseOverHandler(this);
	}
	
	/**
	 * refresh body panel content.
	 */
	private void refreshBodyPanel()
	{
		_bodyPanel.clear();
		_bodyPanel.add(getBodyPanelContent());
	}
	
	/**
	 * get header panel.
	 * 
	 * @return header panel
	 */
	private Panel getTitlePanel()
	{
		Panel panel = new FlowPanel();
		panel.add(_selectedCheck);
		panel.add(new InlineHTML(PostDecorationUtil.getTitleHtml(_post, _decoration)));
		
		return panel;
	}
	
	/**
	 * get decoration panel.
	 * 
	 * @return {@link DecorationPanel}
	 */
	private Panel getToolPanel()
	{
		return new DecorationPanel(_decoration, _post.getNumber());
	}
	
	/**
	 * get body panel content html.
	 * 
	 * @return content html
	 */
	private HTML getBodyPanelContent()
	{
		return new HTML(PostDecorationUtil.getBodyHtml(_post, _decoration));
	}
}
